package com.imagination.applet.domain.adapter.os;

import com.imagination.applet.domain.adapter.ObjectStorageAdapter;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

@Component
public class FileSystemStorage implements ObjectStorageAdapter {

    @Override
    public void putObject(String key, InputStream in) throws Exception {
        File file = new File(key);
        Files.createDirectories(Path.of(file.getParent()));
        try (FileOutputStream outputStream = new FileOutputStream(file)) {
            outputStream.write(in.readAllBytes());
        }
    }
}
